package org.chibandroid.study;

import org.chibandroid.objectvalue.TreasureValues;
import org.chibandroid.constant.TreasureConstant;
import org.chibandroid.database.DatabaseHelper;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class TreasureMenu extends Activity {

	private Button mTreasureSet;
	private Button mTreasureHunt;

	private ArrayAdapter<CharSequence> adpter1;

	private AlertDialog mDialog;
	private Button mTreasureExec;
	private Button mDelete;
	private EditText mTreasureName;
	private EditText mSuccessComment;
	private Spinner mTitle;
	private String mTitleName;

	private DatabaseHelper helper;
	private SQLiteDatabase db;
	private Cursor cur;
	private SQLiteStatement stmt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.treasuremenu);

		mTreasureSet = (Button)findViewById(R.id.btnTreasueSet);
		mTreasureHunt = (Button)findViewById(R.id.btnTreasueHunt);

		mTreasureHunt.setOnClickListener(new BtnContlloer());
		mTreasureSet.setOnClickListener(new BtnContlloer());

		getDataBase();

	}

	@Override
	protected void onRestart() {
		getDataBase();
		super.onRestart();
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		db.close();
		helper.close();
	};

	private void getDataBase(){

		String[] _data = {"初期入力して下さい"};

        helper = new DatabaseHelper(this);
		db = helper.getReadableDatabase();

		cur = db.query("TREASUREMASTER", new String[]{"TREASURENAME","SUCCESSCOMMENT"}, null, null, null, null, "TREASURENAME");
		cur.moveToFirst();
		if(cur.getCount()!=0){
			_data = new String[cur.getCount()];
			for(int i=0;i<_data.length;i++){
				_data[i] = cur.getString(0);
				cur.moveToNext();
			}
		}

		adpter1 = new ArrayAdapter<CharSequence>(this, R.layout.sppiner_item, _data);
		adpter1.setDropDownViewResource(R.layout.sppiner_dropdown_item);
	}

	private class BtnContlloer implements View.OnClickListener{

		private TreasureValues _values;

		@Override
		public void onClick(View v) {

			Intent intent = new Intent();

			switch (v.getId()) {
			case R.id.btnTreasueHunt:
				break;
			case R.id.btnTreasueSet:
				mDialog = new AlertDialog.Builder(v.getContext()).create();
				View _view = LayoutInflater.from(v.getContext()).inflate(R.layout.dialog_layout, null);
				mTreasureExec = (Button)_view.findViewById(R.id.btnExec);
				mDelete = (Button)_view.findViewById(R.id.btnDelete);
				mTreasureName = (EditText)_view.findViewById(R.id.TreasureName);
				mSuccessComment = (EditText)_view.findViewById(R.id.SuccessComment);
				mTitle = (Spinner)_view.findViewById(R.id.spinner1);
				mTitle.setAdapter(adpter1);
				mTitle.setOnItemSelectedListener(new SpinnerController());
				mTreasureExec.setOnClickListener(this);
				mDelete.setOnClickListener(this);
				mDialog.setView(_view);
				mDialog.show();
				break;
			case R.id.btnExec:
				mDialog.dismiss();
				_values = new TreasureValues();
				_values.setTreasureName(mTitleName);
				_values.setSuccessComment(mSuccessComment.getText().toString());
				if(mTreasureName.getText().toString().length() != 0){
					mTitleName = mTreasureName.getText().toString();
					_values.setTreasureName(mTitleName);
					TreasureAdd();
				}

				intent.setClass(getBaseContext(), TreasureSet.class);
				intent.putExtra(TreasureConstant.TREASUREMENU, _values);
				startActivity(intent);
				break;
			case R.id.btnDelete:
					TreasureDelete();
					getDataBase();
					mTitle.setAdapter(adpter1);
				break;
			default:
				break;
			}

		}

		private void TreasureAdd(){

			String _sql = "INSERT INTO TREASUREMASTER VALUES (?, ?);";
			db.beginTransaction();
			try{
				stmt = db.compileStatement(_sql);
				stmt.bindString(1,_values.getTreasureName());
				stmt.bindString(2,_values.getSuccessComment());
				stmt.executeInsert();
				db.setTransactionSuccessful();
			}catch(SQLException e){
				Log.d(TreasureConstant.TREASUREMENU, e.getMessage());
			}finally{
				db.endTransaction();
				stmt.close();
			}
		}

		private void TreasureDelete(){
			String _sql  = "DELETE FROM TREASUREMASTER WHERE TREASURENAME = ?;";
			String _sql2 = "DELETE FROM TREASUREHUNT WHERE TREASURENAME = ?;";
			db.beginTransaction();
			try{
				stmt = db.compileStatement(_sql);
				stmt.bindString(1,mTitleName);
				stmt.execute();
				stmt = db.compileStatement(_sql2);
				stmt.bindString(1,mTitleName);
				stmt.execute();
				db.setTransactionSuccessful();
			}catch(SQLException e){
				Log.d(TreasureConstant.TREASUREMENU, e.getMessage());
			}finally{
				db.endTransaction();
				stmt.close();
			}
		}

	}

	private class SpinnerController implements AdapterView.OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			mTitleName = (String)((Spinner)arg0).getSelectedItem();
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}

	}

}
